import { Scalar as u } from '../../nodes/Scalar.js';
import { stringifyString as d } from '../../stringify/stringifyString.js';
const y = {
  identify: t => t instanceof Uint8Array,
  // Buffer inherits from Uint8Array
  default: !1,
  tag: 'tag:yaml.org,2002:binary',
  /**
   * Returns a Buffer in node and an Uint8Array in browsers
   *
   * To use the resulting buffer as an image, you'll want to do something like:
   *
   *   const blob = new Blob([buffer], { type: 'image/jpeg' })
   *   document.querySelector('#photo').src = URL.createObjectURL(blob)
   */
  resolve(t, e) {
    if (typeof Buffer == 'function') return Buffer.from(t, 'base64');
    if (typeof atob == 'function') {
      const s = atob(t.replace(/[\n\r]/g, '')),
        r = new Uint8Array(s.length);
      for (let n = 0; n < s.length; ++n) r[n] = s.charCodeAt(n);
      return r;
    } else
      return (
        e(
          'This environment does not support reading binary tags; either Buffer or atob is required',
        ),
        t
      );
  },
  stringify({ comment: t, type: e, value: s }, r, n, h) {
    const a = s;
    let o;
    if (typeof Buffer == 'function')
      o =
        a instanceof Buffer
          ? a.toString('base64')
          : Buffer.from(a.buffer).toString('base64');
    else if (typeof btoa == 'function') {
      let i = '';
      for (let f = 0; f < a.length; ++f) i += String.fromCharCode(a[f]);
      o = btoa(i);
    } else
      throw new Error(
        'This environment does not support writing binary tags; either Buffer or btoa is required',
      );
    if ((e || (e = u.BLOCK_LITERAL), e !== u.QUOTE_DOUBLE)) {
      const i = Math.max(
          r.options.lineWidth - r.indent.length,
          r.options.minContentWidth,
        ),
        f = Math.ceil(o.length / i),
        g = new Array(f);
      for (let l = 0, b = 0; l < f; ++l, b += i) g[l] = o.substr(b, i);
      o = g.join(
        e === u.BLOCK_LITERAL
          ? `
`
          : ' ',
      );
    }
    return d({ comment: t, type: e, value: o }, r, n, h);
  },
};
export { y as binary };
